from data_define import Record

import json


class FileRead:
    def read_data(self) -> list[Record]:
        pass


class JsonFileeader(FileRead):
    def __init__(self, path):
        self.path = path

    def read_data(self):
        rest_list: list = []
        f = open(self.path, "r", encoding="UTF-8")
        try:
            num = 0
            for line in f.readlines():
                line = line.strip()
                num = num + 1
                if "CanalSyncDirectReceiver" in line and "message:" in line and '"type":"UPDATE"' in line and "bq_parts_entry" in line:
                    data_list = line.split("message:")
                    str3 = data_list[1]
                    data_list2 = str3.split(",deliveryTag")
                    # print(data_list[1])
                    # data_json = json.loads(line)
                    # record = Record(data_json["date"], data_json["order_id"], int(data_json["money"]),
                    #                 data_json["province"])
                    rest_list.append(data_list2[0])
            print("f(num={})", num)
        except Exception as e:
            print(e)
        finally:
            f.close()
        return rest_list


class TextFileReader(FileRead):
    def __init__(self, path):
        self.path = path

    def read_data(self):
        rest_list: list = []
        f = open(self.path, "r", encoding="UTF-8")
        try:
            for line in f.readlines():
                line = line.strip()
                rest_list.append(line)
        except Exception as e:
            print(e)
        finally:
            f.close()
        return rest_list


if __name__ == '__main__':
    sql_list: list = []
    data_list1: list = []
    id_list: list = []
    file_path1 = "id.txt"
    file_path = "info.log"
    reader1 = TextFileReader(file_path1)
    rest_list1 = reader1.read_data()
    reader = JsonFileeader(file_path)
    rest_list = reader.read_data()
    for str0 in rest_list:
        for str1 in rest_list1:
            if str1 in str0:
                id_list.append(str1)
                data_list1.append(str0)
                break
    num = 0
    for data in data_list1:
        num += 1
        print(data)
        data_json = json.loads(data)
        log_data_json = data_json["data"]
        old_json_list = data_json["old"]
        i = 0
        for data in log_data_json:
            sql_str = "UPDATE bq_parts_entry set "
            data_id = data["id"]
            old_data_json = old_json_list[i]
            i = i + 1
            str8 = ""
            for data in old_data_json:
                print(data)
                str8 = str8 + data + "= \'" + old_data_json[data]+"\' and "
        sql_1= sql_str + str8 + " WHERE id =" + data_id +";"
        sql_list.append(sql_1.replace("and  WHERE","WHERE"))
    print(num)
    for data in id_list:
        print(data)
    print(id_list)

    for sql_str in sql_list:
        print(sql_str)
